#在使用分类器时，要知道导入.xml文件（C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml）

# #图片人脸检测
# import cv2
# img = cv2.imread('C:\\Users\\asus\\Desktop\\111.jpg',1)#读取一张图片
# gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)#将图片转化成灰度
# face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")
# face_cascade.load('C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml')#一定要告诉编译器文件所在的具体位置
# '''此文件是opencv的haar人脸特征分类器'''
# faces = face_cascade.detectMultiScale(gray, 1.3, 5)
# cv2.namedWindow('face')
# for (x,y,w,h) in faces:
#     img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
# cv2.imshow('face',img)
# cv2.waitKey()

# 摄像头人脸检测
def face1(img):
    face_cascade = cv.CascadeClassifier("haarcascade_frontalface_alt2.xml")
    face_cascade.load(
        'C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml')  # 一定要告诉编译器文件所在的具体位置
    '''此文件是opencv的haar人脸特征分类器'''
    faces = face_cascade.detectMultiScale(img, 1.3, 5)
    # cv.namedWindow('face')
    for (x, y, w, h) in faces:
        img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cv.putText(img,'face',(x,y),cv.FONT_HERSHEY_SIMPLEX, 1.2, (1, 3, 250), 2)
    # cv.imshow('face', img)

def eyes(img):
    eye_cascade = cv.CascadeClassifier('haarcascade_eye_tree_eyeglasses.xml')
    eye_cascade.load(
        'C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_eye_tree_eyeglasses.xml')  # 一定要告诉编译器文件所在的具体位置
    faces = eye_cascade.detectMultiScale(img, 1.3, 5)
    cv.namedWindow('eyes')
    for (x, y, w, h) in faces:
        img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        cv.putText(img, 'eye', (x, y), cv.FONT_HERSHEY_SIMPLEX, 1.2, (1, 3, 250), 2)
    cv.imshow('eyes', img)

import cv2 as cv
import numpy as np
capture =cv.VideoCapture(0)
while True:
    ret,frame =capture.read()
    face1(frame)
    eyes(frame)
    cv.waitKey(50)
cv.destroyAllWindows()

#视频人脸检测
# def face1(img):
#     face_cascade = cv.CascadeClassifier("haarcascade_frontalface_alt2.xml")
#     face_cascade.load(
#         'C:\\Users\\asus\\AppData\\Roaming\\Python\\Python37\\site-packages\\cv2\\data\\haarcascade_frontalface_alt2.xml')  # 一定要告诉编译器文件所在的具体位置
#     '''此文件是opencv的haar人脸特征分类器'''
#     faces = face_cascade.detectMultiScale(img, 1.3, 5)
#     cv.namedWindow('face')
#     for (x, y, w, h) in faces:
#         img = cv.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
#         cv.putText(img,'face',(x,y),cv.FONT_HERSHEY_SIMPLEX, 1.2, (1, 3, 250), 2)
#     cv.imshow('face', img)
#
# import cv2 as cv
# import numpy as np
# capture =cv.VideoCapture('C:\\Users\\asus\\Documents\\WeChat Files\\wxid_z8i8k2ydzakq21\\FileStorage\\Video\\2020-11\\f0f4fa5b40ff913754af854e9f6ac506.mp4')
# while True:
#     ret,frame =capture.read()
#     face1(frame)
#     cv.waitKey(50)
# cv.destroyAllWindows()
